<template>
  <div class="refresh-table">
    <div class="refresh-table__icons">
      <el-tooltip
        v-if="!noSearch"
        class="item"
        effect="dark"
        :content="searchVisible ? t('common.hiddenSearch') : t('common.openSearch')"
        placement="top"
      >
        <div
          class="refresh-table__icon"
          :style="{ 'border-color': themeColor }"
          @click="changeSearch"
        >
          <el-icon class="text-sm">
            <Search />
          </el-icon>
        </div>
      </el-tooltip>
      <el-tooltip class="item" effect="dark" :content="t('common.refresh')" placement="top">
        <div
          class="refresh-table__icon"
          :style="{ 'border-color': themeColor }"
          @click="clickRefresh"
        >
          <el-icon class="text-sm">
            <Refresh />
          </el-icon>
        </div>
      </el-tooltip>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineEmits } from 'vue'
import { ElTooltip, ElIcon } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'

// 定义属性
const props = defineProps({
  noSearch: {
    type: Boolean,
    default: false,
  },
  themeColor: {
    type: String,
    default: '#409eff',
  },
  t: {
    type: Function,
    default: (key: string) => key,
  },
})

// 定义组件状态
const searchVisible = ref(true)

// 定义事件
const emit = defineEmits(['searchVisible', 'clickRefresh'])

// 定义方法
const changeSearch = () => {
  searchVisible.value = !searchVisible.value
  emit('searchVisible', searchVisible.value)
}

const clickRefresh = () => {
  emit('clickRefresh')
}

// 默认翻译函数
const t = props.t
</script>

<style lang="scss" scoped>
.refresh-table {
  position: absolute;
  right: 40px;
  top: 4px;
  cursor: pointer;

  &__icons {
    display: flex;
    align-items: center;
    height: 40px;
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: 1px solid #dcdfe6;
    border-radius: 50%;
    margin-right: 5px;
    transition: all 0.3s;
    background-color: var(--el-bg-color);

    &:hover {
      background-color: var(--el-bg-color-page);
      border-color: var(--el-color-primary);
    }

    &:last-child {
      margin-right: 0;
    }
  }
}

.text-sm {
  font-size: 16px;
}
</style>